﻿using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace TwoItems
{
    /// <summary>
    /// 负责数据在数据库中的读写
    /// </summary>
    public class DataGateWay
    {
        public static ExampleInfo Get(int id)
        {
            Database db = DatabaseFactory.CreateDatabase();
            ExampleInfo example = new ExampleInfo();
            string sqlString = "SELECT * FROM data WHERE DataCount = " + id;
            IDataReader reader = db.ExecuteReader(CommandType.Text, sqlString);
            using (reader)
            {
                if (reader.Read())
                {
                    example.Id = id;
                    example.U1 = (double)reader["u1"];
                    example.O1 = (double)reader["o1"];
                    example.E1 = (double)reader["e1"];
                    example.SD1 = (double)reader["sd1"];

                    example.U2 = (double)reader["u2"];
                    example.O2 = (double)reader["o2"];
                    example.E2 = (double)reader["e2"];
                    example.SD2 = (double)reader["sd2"];

                    example.R12 = (double)reader["r12"];
                    example.R21 = (double)reader["r21"];

                    //example.QC1 = (double)reader["qc1"];
                    //example.QC2 = (double)reader["qc2"];
                    //example.PC1 = (double)reader["pc1"];
                    //example.PC2 = (double)reader["pc2"];
                    //example.PC = (double)reader["pc"];

                    //example.QD1 = (double)reader["qd1"];
                    //example.QD2 = (double)reader["qd2"];
                    //example.PD1 = (double)reader["pd1"];
                    //example.PD2 = (double)reader["pd2"];
                    //example.PD = (double)reader["pd"];

                }
            }
            return example;
        }

        /// <summary>
        /// 更新分布式模型的计算结果
        /// </summary>
        /// <param name="exampleInfo"></param>
        public static void UpdateDecentralizedModel(ExampleInfo example)
        {
            Database db = DatabaseFactory.CreateDatabase();
            string sqlString =
                string.Format(
                    "UPDATE data SET qd1 = {0},qd2={1},pd1={2},pd2={3},pd={4},prob1={5},prob2={6},uo1={7},uo2={8},maxQd1={9},maxQd2={10} WHERE DataCount ={11} ",
                    example.QD1, example.QD2, example.PD1, example.PD2, example.PD, example.Prob1, example.Prob2,
                    example.UO1, example.UO2, example.MaxQD1, example.MaxQD2,
                    example.Id);


            db.ExecuteNonQuery(CommandType.Text, sqlString);

        }

        /// <summary>
        /// 更新集中式模型的计算结果
        /// </summary>
        /// <param name="exampleInfo"></param>
        public static void UpdateCentralizedModel(ExampleInfo example)
        {
            Database db = DatabaseFactory.CreateDatabase();
            string sqlString =
                string.Format(
                    "UPDATE data SET qc1 = {0},qc2={1},pc1={2},pc2={3},pc={4},maxQc1={5},maxQc2={6} WHERE DataCount ={7} ",
                    example.QC1,
                    example.QC2, example.PC1,
                    example.PC2, example.PC, example.MaxQC1, example.MaxQC2, example.Id);


            db.ExecuteNonQuery(CommandType.Text, sqlString);

        }
    }
}
